package com.digitalchina.lxnsy.activity.old;

import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.AdHistoryAdapter;
import com.digitalchina.lxnsy.api.BusinessQandA;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.HQDetailsVO;
import com.digitalchina.lxnsy.entity.MsgVO;
import com.digitalchina.lxnsy.interfaces.IRecyclerType;
import com.digitalchina.lxnsy.toolkit.DateStyle;
import com.digitalchina.lxnsy.toolkit.DateUtil;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.jude.easyrecyclerview.EasyRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：byd
 * 创建时间：2017/4/7 16:23
 * 修改备注：
 * ================================================
 */

public class AdvisoryHistoryDetailsActivity extends BaseActivity {
    @Bind(R.id.ad_history_tv_question_name)
    TextView adHistoryTvQuestionName;
    @Bind(R.id.hv_of_ad_history_tips)
    TextView hvOfAdHistoryTips;
    @Bind(R.id.ad_history_tv_expert_name)
    TextView adHistoryTvExpertName;
    @Bind(R.id.ad_history_tv_question_date)
    TextView adHistoryTvQuestionDate;
    @Bind(R.id.advisory_history_recycler_view)
    EasyRecyclerView advisoryHistoryRecyclerView;
    @Bind(R.id.ad_history_ll_question_ok)
    LinearLayout adHistoryLlQuestionOk;
    @Bind(R.id.ad_history_ll_person_service)
    LinearLayout adHistoryLlPersonService;

    private AdHistoryAdapter mAdapter;
    private List<IRecyclerType> allList = new ArrayList<>();
    private List<MsgVO> msgVOList = new ArrayList<>();
    private String userId;
    private String expertName = "";
    private String expertImage = "";

    private Map<String, Object> map;
    private HQDetailsVO hqDetailsVO;

    @Override
    public void setLayout() {
        setTitle("咨询历史");
        setContentLayout(R.layout.activity_advisory_history_details);
    }

    @Override
    public void initMember() {
        if (getIntent().hasExtra("map")) {
            map = (Map<String, Object>) getIntent().getSerializableExtra("map");
            hqDetailsVO = (HQDetailsVO) map.get("HADetailsVO");
        }else { // TODO 测试 不需要可以删除 Welcome的咨询历入口也请删除
            hqDetailsVO = new HQDetailsVO();
            hqDetailsVO.setId("69d98286-8e62-4ad6-8997-f9312e2826d2");
        }
        BusinessQandA.getSimilarQuestionDetail(this, hqDetailsVO.getId(), mHandler); //TODO 真实数据传递
        advisoryHistoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AdHistoryAdapter(this);
        advisoryHistoryRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_SIMILAR_QUESTION_DETAIL_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_SIMILAR_QUESTION_DETAIL_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        hqDetailsVO = FastJsonUtil.getBean(object.toString(), "body", "hisQuestionDetail", HQDetailsVO.class);
        if (null != hqDetailsVO) {
            userId = hqDetailsVO.getUserId();
            expertName = hqDetailsVO.getExpertName();
            expertImage = hqDetailsVO.getExpertImg();
            setContent();
        }
        msgVOList = FastJsonUtil.getListBean(object.toString(), "body", "mes", MsgVO.class);
        if (null != msgVOList && msgVOList.size() > 0) {
            for (int i = 0; i < msgVOList.size(); i++) {
                if (!StringUtil.isStrEmpty(userId) && userId.equals(msgVOList.get(i).getSendUser())) { //提问者
                    msgVOList.get(i).setType(AdHistoryAdapter.ITEM_TYPE_USER);
                } else { // 回答者(专家)
                    msgVOList.get(i).setType(AdHistoryAdapter.ITEM_TYPE_EXPERT);
                    msgVOList.get(i).setName(expertName);
                    msgVOList.get(i).setImage(expertImage);
                }
            }

        }

        mAdapter.addAll(msgVOList);

    }

    /**
     * 填充数据
     */
    private void setContent() {
        adHistoryTvQuestionName.setText(StringUtil.isStrEmpty(hqDetailsVO.getContent()) ? "" : hqDetailsVO.getContent().replace("\n", "").split("：")[1]); //提取问题
        adHistoryTvExpertName.setText(StringUtil.isStrEmpty(hqDetailsVO.getExpertName()) ? "" : hqDetailsVO.getExpertName());
        adHistoryTvQuestionDate.setText(StringUtil.isStrEmpty(hqDetailsVO.getStartTime()) ? "" : DateUtil.StringToString(hqDetailsVO.getStartTime(), DateStyle.YYYY_MM_DD_EN));
    }

    @Override
    public void rightOnClick() {

    }


    @OnClick({R.id.ad_history_ll_question_ok, R.id.ad_history_ll_person_service})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ad_history_ll_question_ok:
                GotoUtil.gotoActivity(AdvisoryHistoryDetailsActivity.this, IssueEvaluateActivity.class, "map", map);
                break;
            case R.id.ad_history_ll_person_service:
                GotoUtil.gotoActivity(AdvisoryHistoryDetailsActivity.this, ChooseExpertsListActivity.class, "map", map);
                break;
        }
    }
}
